package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;
import com.tul.exercise.base.utils.MathUtils;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

/**
 * 滑块与进度条
 */
public class Progress extends JFrame {
    private JProgressBar pb = new JProgressBar();

    /**
     * 进度条
     */
    private ProgressMonitor pm = new ProgressMonitor(
            this, "", "", 0, 100);

    /**
     * 滑块
     */
    private JSlider sb = new JSlider(JSlider.HORIZONTAL, 0, 100, 60);

    public Progress() {
        setLayout(new GridLayout(2, 1));
        add(pb);
        pm.setProgress(0);
        pm.setMillisToPopup(1000);
        sb.setValue(0);
        sb.setPaintTicks(true);
        sb.setMajorTickSpacing(20);
        sb.setMinorTickSpacing(5);
        sb.setBorder(new TitledBorder("Slide Me"));
        // 共享模型
        sb.setModel(sb.getModel());

        add(sb);
        sb.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {

                int value = MathUtils.getRandomInt(0, sb.getValue());
                pm.setProgress(value);
                System.out.println(value);
            }
        });

    }

    public static void main(String[] args) {
        SwingConsole.run(new Progress(), 800, 500);
    }
}
